import { ref } from "vue";


export function useCurrentView(): CurrentViewResult{
    const currentView = ref<boolean>(false)

    function tiggleCurrentView(status: boolean) {
        currentView.value = status
    }

    function checkCurrentView(): boolean{
        return currentView.value === true
    }

    return { tiggleCurrentView, checkCurrentView }
}

export function useSelectCellState(): SelectCellStateResult {
    const selectCellState = ref<SelectCellState>({ });

    function checkSelected(rowindex?: number, colindex?: number) {
        const {startX, endX, startY, endY } = selectCellState.value

        if ( startX === undefined || endX === undefined || startY === undefined || endY === undefined || rowindex === undefined || colindex === undefined) return false

        const minx = Math.min(startX, endX)
        const maxx = Math.max(startX, endX)
        const miny = Math.min(startY, endY)
        const maxy = Math.max(startY, endY)

        return rowindex >= miny && rowindex <= maxy && colindex >= minx && colindex <= maxx
    }

    function checkSelectedRowindex(rowindex?: number) {
        const {startX, endX, startY, endY } = selectCellState.value

        if ( startX === undefined || endX === undefined || startY === undefined || endY === undefined || rowindex === undefined) return false

        const miny = Math.min(startY, endY)
        const maxy = Math.max(startY, endY)

        return rowindex >= miny && rowindex <= maxy
    }

    function startSelect(rowindex?: number, colindex?: number){
        selectCellState.value.startX = colindex
        selectCellState.value.endX = colindex
        selectCellState.value.startY = rowindex
        selectCellState.value.endY = rowindex
    }
    function moveSelect(rowindex?: number, colindex?: number){
        selectCellState.value.endX = colindex
        selectCellState.value.endY = rowindex
    }

    function checkSelectedFirstColCell(rowindex?: number, colindex?: number): boolean {
        const {startX, endX } = selectCellState.value
        return checkSelected(rowindex, colindex) && colindex === Math.min(startX!, endX!)
    }
    function checkSelectedLastColCell(rowindex?: number, colindex?: number): boolean {
        const {startX, endX } = selectCellState.value
        return checkSelected(rowindex, colindex) && colindex === Math.max(startX!, endX!)
    }

    function checkSelectedFirstRowCell(rowindex?: number, colindex?: number): boolean {
        const {startY, endY } = selectCellState.value
        return checkSelected(rowindex, colindex) && rowindex === Math.min(startY!, endY!)
    }
    function checkSelectedLastRowCell(rowindex?: number, colindex?: number): boolean {
        const {startY, endY } = selectCellState.value
        return checkSelected(rowindex, colindex) && rowindex === Math.max(startY!, endY!)
    }

    function resetSelectState() {
        selectCellState.value.startX = undefined
        selectCellState.value.endX = undefined
        selectCellState.value.startY = undefined
        selectCellState.value.endY = undefined
    }
   

    return {
        selectCellState,
        checkSelected,
        checkSelectedRowindex,
        startSelect,
        moveSelect,
        checkSelectedFirstRowCell,
        checkSelectedLastRowCell,
        checkSelectedFirstColCell,
        checkSelectedLastColCell,
        resetSelectState
    }
}

export function useEditCellState(): EditCellStateResult {
    const editCellState = ref<EditCellState>({})

    function readyEdit(rowindex?: number, colindex?: number) : void {
        editCellState.value.rowindex = rowindex;
        editCellState.value.colindex = colindex;
        editCellState.value.status = 'ready'
    }

    function doingEdit(rowindex?: number, colindex?: number) : void {
        editCellState.value.rowindex = rowindex;
        editCellState.value.colindex = colindex;
        editCellState.value.status = 'doing'
    }

    function checkReadyEdit(rowindex?: number, colindex?: number) {
        if (rowindex === undefined || colindex === undefined) return false
        return editCellState.value.rowindex === rowindex && editCellState.value.colindex === colindex && editCellState.value.status === 'ready'
    }

    function checkDoingEdit(rowindex?: number, colindex?: number) {
        if (rowindex === undefined || colindex === undefined) return false
        return editCellState.value.rowindex === rowindex && editCellState.value.colindex === colindex && editCellState.value.status === 'doing'
    }

    function resetEditState(){
        editCellState.value.rowindex = undefined;
        editCellState.value.colindex = undefined;
        editCellState.value.status = undefined
    }


    return { 
        editCellState,
        readyEdit,
        doingEdit,
        checkReadyEdit,
        checkDoingEdit,
        resetEditState
    }
}

export function useContextmenu(): ContextmenuResult {
    const visibleContextmenu = ref<boolean>(false)
    const positionContextmenu = ref<PositionContextmenu>({ left: 0, top: 0 })

    function tiggleVisibleContextmenu(status: boolean){
        visibleContextmenu.value = status
    }

    function checkVisibleContextmenu(){
        return visibleContextmenu.value === true
    }

    function setPositionContextmenu(position: PositionContextmenu){
        Object.assign(positionContextmenu.value, position)
    }

    return {
        visibleContextmenu,
        positionContextmenu,
        tiggleVisibleContextmenu,
        checkVisibleContextmenu,
        setPositionContextmenu
    }
}